package servlet;

import utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet(name = "UpdateStudentServlet", value = "/UpdateStudentServlet")
public class UpdateStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /**
         * 修改学生信息操作
         */
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");

        String thisid = request.getParameter("id");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String major = request.getParameter("major");
        int age = Integer.parseInt(request.getParameter("age"));

        String sql="update student set name=?,password=?,sex=?,major=?,age=? where id=?";
        try {
            PreparedStatement ps = JDBCUtils.getconnection().prepareStatement(sql);
            ps.setString(6,thisid);
            ps.setString(1, name);
            ps.setString(2, password);
            ps.setString(3, sex);
            ps.setString(4, major);
            ps.setInt(5, age);
            int sout = ps.executeUpdate();
            System.out.println("updatesout = "+sout);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.getRequestDispatcher("all.jsp").forward(request, response);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);
    }
}
